package sg.edu.nus.iss.team8s.vmcs.builder;
/**
 * 
 * @author Zhou Hui
 * 
 */
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;

import sg.edu.nus.iss.team8s.vmcs.Listener.CoinDisplayListener;
import sg.edu.nus.iss.team8s.vmcs.Listener.DrinkDisplayListener;
import sg.edu.nus.iss.team8s.vmcs.beans.Coin;
import sg.edu.nus.iss.team8s.vmcs.beans.StoreItem;
import sg.edu.nus.iss.team8s.vmcs.beans.StoreObject;
import sg.edu.nus.iss.team8s.vmcs.cache.CashStore;
import sg.edu.nus.iss.team8s.vmcs.cache.DrinksStore;
import sg.edu.nus.iss.team8s.vmcs.commons.CommonsFactory;
import sg.edu.nus.iss.team8s.vmcs.commons.Constant;
import sg.edu.nus.iss.team8s.vmcs.control.MaintenanceController;

public class MaintenanceBuilder implements UIBuilder {
	
	public static final String MAINTAINER_PANEL_TITLE = "VMCS - Maintainer Panel";
	public static final String TOTAL_CASH_BUTTON_TEXT="Show Total Cash Held";
	public static final String COLLECT_CASH_BUTTON_TEXT="Press to Collect All Cash";
	public static final String FINISH_BUTTON="Press Here when Finish";
	private MaintenancePanel maintenancePanel;
	
	private MaintenanceController maintenanceController;
	
	private Label[] lblListCoinQty;
	private Label[] lblListDrinkQty;
	private Label lblTotalValue;
	public MaintenanceBuilder()
	{
		maintenancePanel = new MaintenancePanel();
		maintenanceController = MaintenanceController.getInstance();
	}

	public void buildInputPanel() {
		// TODO Auto-generated method stub
		Panel p = new Panel();
		p.setPreferredSize(new Dimension(360, 60));
		p.setLayout(null);
		p.setBackground(Constant.DEFAULT_BACKGROUND_COLOR);
		Insets insets = p.getInsets();
		CommonsFactory factory=CommonsFactory.getInstance();
		Label textLabel = factory.getLabel(Constant.LABEL_WITH_NORMAL_BGRD, "Password:");
		textLabel.setFont(new Font("Times New Roman", Font.BOLD, 13));
		textLabel.setSize(80, 25);
		Dimension d = textLabel.getPreferredSize();
		textLabel.setBounds(90 + insets.left, 10 + insets.top, d.width, d.height);
		TextField textField = new TextField(10);
		textField.setSize(80, 25);
		textField.setEchoChar('*');
		textField.setText("111");
		d = textField.getPreferredSize();
		textField.setBounds(180 + insets.left, 10 + insets.top, d.width, d.height);
		Label valid = factory.getLabel(Constant.LABEL_WITH_GREEN_BGRD, "Valid Password");
		valid.setFont(new Font("Times New Roman", Font.BOLD, 14));
		valid.setForeground(Color.WHITE);
		valid.setSize(110, 25);
		d = valid.getPreferredSize();
		valid.setBounds(50 + insets.left, 40 + insets.top, d.width, d.height);
		Label invalid = factory.getLabel(Constant.LABEL_WITH_RED_BGRD, "Invalid Password");
		invalid.setFont(new Font("Times New Roman", Font.BOLD, 14));
		invalid.setForeground(Color.GRAY);
		invalid.setSize(125, 25);
		d = invalid.getPreferredSize();
		invalid.setBounds(180 + insets.left, 40 + insets.top, d.width, d.height);
		
		p.add(textLabel);
		p.add(textField);
		p.add(valid);
		p.add(invalid);
		
		maintenancePanel.setInputPanel(p);
		maintenancePanel.setPassword(textField);
		maintenancePanel.setValidPassword(valid);
		maintenancePanel.setValidPassword(invalid);
		maintenancePanel.add(p);
		
	}

	public void buildStatusPanel() {
		// TODO Auto-generated method stub
		Panel p = new Panel();
		p.setLayout(null);
		//p.setPreferredSize(new Dimension(Constant.PANEL_WIDTH, 60));
		p.setPreferredSize(new Dimension(360, 200));
		//p.setBackground(Color.YELLOW);
		CommonsFactory factory=CommonsFactory.getInstance();
		p.setBackground(Constant.DEFAULT_BACKGROUND_COLOR);
		Insets insets = p.getInsets();
		Button b1= factory.getButton(Constant.BUTTON_LONG, MaintenanceBuilder.TOTAL_CASH_BUTTON_TEXT);
		b1.setSize(160, 30);
		b1.setFont(new Font("Times New Roman", Font.BOLD, 14));
		Dimension d = b1.getPreferredSize();
		b1.setBounds(40 + insets.left, 20 + insets.top, d.width, d.height);
		p.add(b1);
		
		int value = 0;
		CashStore cashStore = CashStore.getInstance();
		for(int i =0;i<cashStore.getStoreSize();i++){
			StoreItem item = cashStore.getItem(i);
			Coin obj = (Coin)item.getContent();
			value += item.getQuantity() * obj.getValue();
		}
		
		Label L2= factory.getLabel(Constant.LABEL_WITH_BLACK_BGRD_LONGER, String.valueOf(value));
		d = L2.getPreferredSize();
		L2.setAlignment(Label.CENTER);
		L2.setForeground(Color.YELLOW);
		L2.setBounds(220 + insets.left, 20 + insets.top, d.width, d.height);
		p.add(L2);
		
		maintenanceController.setlblTotalValue(L2);
		
		Button b2= factory.getButton(Constant.BUTTON_LONG, MaintenanceBuilder.COLLECT_CASH_BUTTON_TEXT);
		b2.setFont(new Font("Times New Roman", Font.BOLD, 14));
		b2.setSize(180, 30);
		d = b2.getPreferredSize();
		b2.setBounds(100 + insets.left, 60 + insets.top, d.width, d.height);
		p.add(b2);
		
		Label L3= factory.getLabel(Constant.LABEL_WITH_NORMAL_BGRD, "Collect Cash:");
		d = L3.getPreferredSize();
		L3.setFont(new Font("Times New Roman", Font.BOLD, 13));
		L3.setBounds(100 + insets.left, 100 + insets.top, d.width, d.height);
		p.add(L3);
		
		Label L4= factory.getLabel(Constant.LABEL_WITH_BLACK_BGRD_LONGER, "0 C");
		d = L4.getPreferredSize();
		L4.setAlignment(Label.CENTER);
		L4.setForeground(Color.YELLOW);
		L4.setBounds(200 + insets.left, 100 + insets.top, d.width, d.height);
		p.add(L4);
		
		Button b3= factory.getButton(Constant.BUTTON_LONG, MaintenanceBuilder.FINISH_BUTTON);
		b3.setFont(new Font("Times New Roman", Font.BOLD, 14));
		b3.setSize(180, 30);
		d = b3.getPreferredSize();
		b3.setBounds(100 + insets.left, 140 + insets.top, d.width, d.height);
		p.add(b3);
		
		maintenancePanel.setStatusPanel(p);
		maintenancePanel.setShowCashButton(b1);
		maintenancePanel.setCollectCashButton(b2);
		maintenancePanel.setFinishButton(b3);
		maintenancePanel.setShowCashLabel(L2);
		maintenancePanel.setCollectCashLabel(L4);
		maintenancePanel.add(p);
		
	}

	public void buildTitlePanel() {
		// TODO Auto-generated method stub
		Panel p = new Panel();
		p.setLayout(null);
		//p.setPreferredSize(new Dimension(Constant.PANEL_WIDTH, 60));
		p.setPreferredSize(new Dimension(360, 50));
		//p.setBackground(Color.YELLOW);
		p.setBackground(Constant.DEFAULT_BACKGROUND_COLOR);
		Insets insets = p.getInsets();
		Label title = CommonsFactory.getInstance().getLabel(Constant.LABEL_TITLE, MaintenanceBuilder.MAINTAINER_PANEL_TITLE);
		title.setFont(new Font("Times New Roman", Font.BOLD, 20));
		Dimension d = title.getPreferredSize();
		title.setBounds(50 + insets.left, 10 + insets.top, d.width, d.height);
		p.add(title);
		
		this.maintenancePanel.setTitlePanel(p);
		this.maintenancePanel.add(p);
	}
	
	public void buildDisplayPanel()
	{
		Panel p = new Panel();
		p.setSize(360,410);
		p.setLayout(null);
		Insets insets = p.getInsets();
		CommonsFactory factory=CommonsFactory.getInstance();
		Label header = factory.getLabel(Constant.LABEL_WITH_NORMAL_BGRD, "Quantity of Coins Available");
		header.setSize(250, 30);
		header.setFont(new Font("Times New Roman", Font.BOLD, 15));
		Dimension d = header.getPreferredSize();
		header.setBounds(5 + insets.left, 10 + insets.top, d.width, d.height);
		p.add(header);
		
		CashStore cashStore = CashStore.getInstance();
		lblListCoinQty = new Label[cashStore.getStoreSize()];
		
		int GAPLENGTH = 0;
		for(int i =0;i<cashStore.getStoreSize();i++){
			StoreItem item = cashStore.getItem(i);
			StoreObject obj = item.getContent();
			
			Button btnCoin = factory.getButton(Constant.BUTTON_LONG, obj.getName());
			d = btnCoin.getPreferredSize();
			btnCoin.setBounds(5 + insets.left, 40 + GAPLENGTH + insets.top, d.width, d.height);
			
			
			Label lblQty = factory.getLabel(Constant.LABEL_WITH_BLACK_BGRD_SHORTER, String.valueOf(item.getQuantity()));
			lblQty.setForeground(Color.YELLOW);
			lblQty.setAlignment(Label.CENTER);
			lblQty.setVisible(false);
			d = lblQty.getPreferredSize();
			lblQty.setBounds(180 + insets.left, 40 + GAPLENGTH + insets.top, d.width, d.height);
			
			lblListCoinQty[i] = lblQty;
			
			btnCoin.addActionListener(new CoinDisplayListener(i,lblListCoinQty));
			
			p.add(btnCoin);
			p.add(lblQty);
			GAPLENGTH += 30;
		}
		
		maintenanceController.setlblListCoinQty(lblListCoinQty);
		
		/*Button b1 = factory.getButton(Constant.BUTTON_LONG, "5 c");
		d = b1.getPreferredSize();
		b1.setBounds(5 + insets.left, 40 + insets.top, d.width, d.height);
		Label L1= factory.getLabel(Constant.LABEL_WITH_BLACK_BGRD_SHORTER, "22");
		d = L1.getPreferredSize();
		L1.setAlignment(Label.CENTER);
		L1.setForeground(Color.YELLOW);
		L1.setBounds(180 + insets.left, 40 + insets.top, d.width, d.height);
		p.add(b1);
		p.add(L1);
		
		Button b2 = factory.getButton(Constant.BUTTON_LONG, "10 c");
		d = b1.getPreferredSize();
		b2.setBounds(5 + insets.left, 70 + insets.top, d.width, d.height);
		Label L2= factory.getLabel(Constant.LABEL_WITH_BLACK_BGRD_SHORTER, "22");
		L2.setForeground(Color.YELLOW);
		L2.setAlignment(Label.CENTER);
		d = L2.getPreferredSize();
		L2.setBounds(180 + insets.left, 70 + insets.top, d.width, d.height);
		p.add(b2);
		p.add(L2);
		
		Button b3 = factory.getButton(Constant.BUTTON_LONG, "20 c");
		d = b3.getPreferredSize();
		b3.setBounds(5 + insets.left, 100 + insets.top, d.width, d.height);
		Label L3= factory.getLabel(Constant.LABEL_WITH_BLACK_BGRD_SHORTER, "22");
		L3.setForeground(Color.YELLOW);
		L3.setAlignment(Label.CENTER);
		d = L3.getPreferredSize();
		L3.setBounds(180 + insets.left, 100 + insets.top, d.width, d.height);
		p.add(b3);
		p.add(L3);
		
		Button b4 = factory.getButton(Constant.BUTTON_LONG, "50 c");
		d = b4.getPreferredSize();
		b4.setBounds(5 + insets.left, 130 + insets.top, d.width, d.height);
		Label L4= factory.getLabel(Constant.LABEL_WITH_BLACK_BGRD_SHORTER, "22");
		L4.setForeground(Color.YELLOW);
		L4.setAlignment(Label.CENTER);
		d = L4.getPreferredSize();
		L4.setBounds(180 + insets.left, 130 + insets.top, d.width, d.height);
		p.add(b4);
		p.add(L4);
		
		Button b5 = factory.getButton(Constant.BUTTON_LONG, "$ 1");
		d = b5.getPreferredSize();
		b5.setBounds(5 + insets.left, 160 + insets.top, d.width, d.height);
		Label L5= factory.getLabel(Constant.LABEL_WITH_BLACK_BGRD_SHORTER, "22");
		L5.setForeground(Color.YELLOW);
		L5.setAlignment(Label.CENTER);
		d = L5.getPreferredSize();
		L5.setBounds(180 + insets.left, 160 + insets.top, d.width, d.height);
		p.add(b5);
		p.add(L5);*/
		
		
		Label header1 = factory.getLabel(Constant.LABEL_WITH_NORMAL_BGRD, "Quantity of Drinks Available");
		header1.setSize(250, 30);
		header1.setFont(new Font("Times New Roman", Font.BOLD, 15));
		 d = header1.getPreferredSize();
		header1.setBounds(5 + insets.left, 200 + insets.top, d.width, d.height);
		p.add(header1);
		
		GAPLENGTH = 0;
		DrinksStore drinkStore = DrinksStore.getInstance();
		lblListDrinkQty = new Label[drinkStore.getStoreSize()];
		
		for(int i =0;i<drinkStore.getStoreSize();i++){
			StoreItem item = drinkStore.getItem(i);
			StoreObject obj = item.getContent();
			
			Button btnDrink = factory.getButton(Constant.BUTTON_LONG, obj.getName());
			d = btnDrink.getPreferredSize();
			btnDrink.setBounds(5 + insets.left, 230 + GAPLENGTH + insets.top, d.width, d.height);
			
			Label lblDrink= factory.getLabel(Constant.LABEL_WITH_BLACK_BGRD_SHORTER, String.valueOf(item.getQuantity()));
			lblDrink.setAlignment(Label.CENTER);
			lblDrink.setForeground(Color.YELLOW);
			d = lblDrink.getPreferredSize();
			lblDrink.setBounds(180 + insets.left, 230 + GAPLENGTH + insets.top, d.width, d.height);
			lblDrink.setVisible(false);
			
			lblListDrinkQty[i] = lblDrink;
			
			btnDrink.addActionListener(new DrinkDisplayListener(i,lblListDrinkQty));
			
			p.add(btnDrink);
			p.add(lblDrink);
			
			GAPLENGTH += 30;
		}
		maintenanceController.setlblListDrinkQty(lblListDrinkQty);
		
		/*Button b11 = factory.getButton(Constant.BUTTON_LONG, "Coca-Cola");
		d = b11.getPreferredSize();
		b11.setBounds(5 + insets.left, 230 + insets.top, d.width, d.height);
		Label L11= factory.getLabel(Constant.LABEL_WITH_BLACK_BGRD_SHORTER, "22");
		L11.setAlignment(Label.CENTER);
		L11.setForeground(Color.YELLOW);
		d = L11.getPreferredSize();
		L11.setBounds(180 + insets.left, 230 + insets.top, d.width, d.height);
		p.add(b11);
		p.add(L11);
		
		Button b22 = factory.getButton(Constant.BUTTON_LONG, "Fanta");
		d = b22.getPreferredSize();
		b22.setBounds(5 + insets.left, 260 + insets.top, d.width, d.height);
		Label L22= factory.getLabel(Constant.LABEL_WITH_BLACK_BGRD_SHORTER, "22");
		L22.setAlignment(Label.CENTER);
		L22.setForeground(Color.YELLOW);
		d = L22.getPreferredSize();
		L22.setBounds(180 + insets.left, 260 + insets.top, d.width, d.height);
		p.add(b22);
		p.add(L22);
		
		Button b33 = factory.getButton(Constant.BUTTON_LONG, "Sarsi");
		d = b33.getPreferredSize();
		b33.setBounds(5 + insets.left, 290 + insets.top, d.width, d.height);
		Label L33= factory.getLabel(Constant.LABEL_WITH_BLACK_BGRD_SHORTER, "22");
		L33.setAlignment(Label.CENTER);
		L33.setForeground(Color.YELLOW);
		d = L33.getPreferredSize();
		L33.setBounds(180 + insets.left, 290 + insets.top, d.width, d.height);
		p.add(b33);
		p.add(L33);
		
		Button b44 = factory.getButton(Constant.BUTTON_LONG, "Soya Bean");
		d = b44.getPreferredSize();
		b44.setBounds(5 + insets.left, 320 + insets.top, d.width, d.height);
		Label L44= factory.getLabel(Constant.LABEL_WITH_BLACK_BGRD_SHORTER, "22");
		L44.setAlignment(Label.CENTER);
		L44.setForeground(Color.YELLOW);
		d = L44.getPreferredSize();
		L44.setBounds(180 + insets.left, 320 + insets.top, d.width, d.height);
		p.add(b44);
		p.add(L44);
		
		Button b55 = factory.getButton(Constant.BUTTON_LONG, "Coca-Cola");
		d = b55.getPreferredSize();
		b55.setBounds(5 + insets.left, 350 + insets.top, d.width, d.height);
		Label L55= factory.getLabel(Constant.LABEL_WITH_BLACK_BGRD_SHORTER, "22");
		L55.setAlignment(Label.CENTER);
		L55.setForeground(Color.YELLOW);
		d = L55.getPreferredSize();
		L55.setBounds(180 + insets.left, 350 + insets.top, d.width, d.height);
		p.add(b55);
		p.add(L55);
		*/
		Label L6=factory.getLabel(Constant.LABEL_WITH_NORMAL_BGRD, "Brand Price");
		L6.setSize(100, 25);
		d = L6.getPreferredSize();
		L6.setFont(new Font("Times New Roman", Font.BOLD, 15));
		L6.setBounds(70 + insets.left, 380 + insets.top, d.width, d.height);
		TextField textField1 = new TextField(20);
		textField1.setSize(50, 25);
		textField1.setText("75 c");
		d = textField1.getPreferredSize();
		textField1.setBounds(180 + insets.left, 380 + insets.top, d.width, d.height);
		p.add(L6);
		p.add(textField1);
		//p.setBackground(Color.BLUE);
		p.setBackground(Constant.DEFAULT_BACKGROUND_COLOR);
		/*
		Button[] buttons = new Button[]{b11, b22, b33, b44, b55};
		Label[] prices = new Label[]{L11, L22, L33, L44, L55};
		
		maintenancePanel.setDisplayPanel(p);
		maintenancePanel.setFiveCent(b1);
		maintenancePanel.setTenCent(b2);
		maintenancePanel.setTwentyCent(b3);
		maintenancePanel.setFiftyCent(b4);
		maintenancePanel.setOneDollar(b5);
		maintenancePanel.setLabelFiveCent(L1);
		maintenancePanel.setLabelTenCent(L2);
		maintenancePanel.setLabelTwentyCent(L3);
		maintenancePanel.setLabelFiftyCent(L4);
		maintenancePanel.setLabelOneDollar(L5);
		maintenancePanel.setBrands(buttons);
		maintenancePanel.setPrices(prices);
		maintenancePanel.setBrandPrice(textField1);*/
		maintenancePanel.add(p);
	}

	public Panel getResult() {
		// TODO Auto-generated method stub
		return this.maintenancePanel;
	}
}
